package com.yuan.algorithms.arithmetic_1;



public class Cell {
	int x = 0;//单元所在行
	int y = 0;//单元所在列
	boolean visited = false;//记录是否访问过
	char c = ' ';//是墙1、可通路0或起点到终点的路径*
	public Cell(int x, int y, char c, boolean visited) {
		this.x = x;
		this.y = y;
		this.c = c;
		this.visited = visited;
	}
	
	/**
	 * 
	 * @param maze 迷宫地图
	 * @param sx 起点x坐标
	 * @param sy 起点y坐标
	 * @param ex 终点x坐标
	 * @param ey 终点y坐标
	 */
	public void mazeExit(char[][] maze, int sx, int sy, int ex, int ey) {
		Cell[][] cells = createMaze(maze);//创建迷宫
		printMaze(cells);// 打印迷宫
		StackSLinked s = new StackSLinked();
		Cell startCell = cells[sx][sy];// 起点
		Cell endCell = cells[ex][ey];//终点
		s.push(startCell);// 起点入栈
		startCell.visited = true;// 标记起点已被访问
		while(!s.isEmpty()) {
			Cell current = (Cell)s.peek();
			if (current == endCell) {// 路径找到
				while (!s.isEmpty()) {
					Cell cell = (Cell)s.pop();//沿路返回将路径上的单元设为*
					cell.c = '*';
					
				}
			}
		}
	}

	private void printMaze(Cell[][] cells) {
		for (int x = 0; x < cells.length; x++) {
			for (int y = 0; y < cells.length; y++) {
				System.out.print(cells[x][y].c);
			}
			System.out.println();
		}
	}

	private Cell[][] createMaze(char[][] maze) {
		Cell[][] cells = new Cell[maze.length][];
		for (int x = 0; x < maze.length; x++) {
			char[] row = maze[x];
			cells[x] = new Cell[row.length];
			for (int y = 0; y < row.length; y++) {
				cells[x][y] = new Cell(x,y,maze[x][y],false);
			}
		}
		return cells;
	}
}
